/*
$(document).ready(function(){
  $("#startWork").click(function(){
    $("#result").load('start-work.php?case_id=' + document.getElementById("case_id").value);
  });
  
  $("#stopWork").click(function(){
    $("#result").load('stop-work.php');
  });

  $("#getCases").click(function(){
    $("#result").load('get-cases.php');
  });
  
});
*/

function theDate() {

	var currentTime = new Date();
	var hours = currentTime.getHours();
	var minutes = currentTime.getMinutes();
	
	var suffix = "AM";
	if (hours >= 12) {
		suffix = "PM";
		hours = hours - 12;
	}
	if (hours == 0) {
		hours = 12;
	}
	
	if (minutes < 10)
	minutes = "0" + minutes
	
	return hours + ":" + minutes + " " + suffix;

}

function quickStart($ixBug) {
	document.getElementById("timerix").innerHTML = $ixBug;
	startTimer();
}

function loadCases($num,$dom) {
	$url = 'get-cases.php?num=' + $num;
	$($dom).load($url);	
}

function setTimerIx() {
	$ix = document.getElementById("enterix").value;
	if ($ix!='')
		document.getElementById("timerix").innerHTML = $ix;	
}

function startTimer() {
	document.getElementById("timerStart").value = theDate();
	document.getElementById("timerStop").value = '';
	loadCases(15,'#chooseCase');
}

function saveTimer() {
	if (document.getElementById("timerix").innerHTML == '???') {
			alert('Please select a case.');
			exit();
		}
	document.getElementById("timerStop").value = theDate();

	$ixBug = document.getElementById("timerix").innerHTML;
	$dStart = document.getElementById("timerStart").value;
	$dStop = document.getElementById("timerStop").value;
	
	$url = 'save-time.php?dStart=' + encodeURIComponent($dStart) + '&dStop=' + encodeURIComponent($dStop) + '&ix=' + $ixBug;
	$("#ajax").load($url);
	//alert($url);
	
	$message = "Saving timer for Case " + $ixBug + " (" + $dStart + " - " + $dStop + ")";
	displayMessage($message);
}

function stopWorking() {
	$message = "No longer working...";
	$("#ajax").load('stop-work.php');
	displayMessage($message);
}

function displayMessage($message) {
	document.getElementById("result").innerHTML = $message;
	setTimeout("resetMessage()",2000);
}

function resetMessage() {
	document.getElementById("result").innerHTML = "<p>Easily and quickly track time against your FogBugz cases.</p>";
}
